//============================================================================
// Name        : lab4_1.cpp
// Author      : 洪春晓
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include "FileProcessor.cpp"
#include <iostream>
using namespace std;

#define INPUTFILENAME "input.txt"
#define OUTPUTFILENAME "output.txt"
void getData(int *(&distance),int *n,int *k);
template<class Type> void outputData(Type data);

int minRefuelNum(int *distance ,int n,int k);//得到最少加油次数

int main()
{

	int *distance;
	int num = 0;//加油次数
	int n;
	int k;
	getData(distance,&n,&k);
	cout<<"加满油后可行驶："<<n<<"公里,"<<"旅途中有"<<k<<"个加油站。"<<endl;
	cout<<"加油站之间的距离分别为：";
    for(int i=0;i<=k;i++)
	 	cout<<distance[i]<<" ";
	cout<<endl;

    num =  minRefuelNum(distance , n, k);
    if(num>=0)
    	outputData(num);
    else
    	outputData("No Solution");
	return 0;
}
void getData(int *(&distance),int *n,int *k)
{
	FileProcessor fp;
	vector<int> data;
	fp.getdata(INPUTFILENAME,data);
	*n = data[0];
	*k = data[1];
	distance = new int [*k+1];
	for(vector<int>::size_type  i=2;i < data.size();i++)
	{
		distance[i-2] = data[i];
	}
}
template<class Type> void outputData(Type data)
{
	FileProcessor fp;
	fp.output_data(OUTPUTFILENAME,data);
}
int minRefuelNum(int *distance ,int n,int k)
{
	int num = 0;
	int temp = n;
	for(int i=0; i<= k ;i++)
		if(distance[i] > n)
		{
			cout<<"No Solution"<<endl;
			return -1;
		}
	for(int i=0; i<=k ;i++)
	{
		if(temp - distance[i] >=0)
			temp -= distance[i];
		else
		{
			num++;
			temp = n-distance[i];
		}
	}
	cout<<"最少需要加油的次数为："<<num<<endl;
	return num;
}
